package com.systop.controller;

import com.systop.domain.vo.CartChangeVo;
import com.systop.domain.vo.CartItem;
import com.systop.domain.vo.CartVo;
import com.systop.entity.Goods;
import com.systop.entity.GoodsStock;
import com.systop.service.GoodsService;
import com.systop.service.GoodsStockService;
import com.systop.service.ShopCartService;
import com.systop.service.SysUserService;
import com.systop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author: jsj
 * @Date: 2023/3/11
 */
@RestController
@RequestMapping("/cart")
public class ShopCarController {

    @Autowired
    ShopCartService shopCartService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsStockService goodsStockService;

    /**
     * 查询购物车信息
     * @return
     */
    @GetMapping("/cartList")
    public R getCartList(){
        CartVo cartList = shopCartService.getCartList();
        return R.ok().put("cartList",cartList);
    }

    /**
     * 添加商品到购物车
     * @param goodsId   商品ID
     * @param goodsNum  商品数量
     * @return
     */
    @GetMapping("/addToCart")
    public R addToCart(@RequestParam("goodsId") Long goodsId,
                       @RequestParam("goodsNum") Integer goodsNum){
        if (goodsService.getById(goodsId) == null) {
            return R.error("商品不存在");
        }

        GoodsStock goodStock = goodsStockService.getStockByGoodsId(goodsId);
        if((goodStock.getStockNum() - goodStock.getLockStockNum())-goodsNum<0){
            return R.error("商品库存不足");
        }

        CartItem cartItem = shopCartService.addToCart(goodsId,goodsNum);

        if (cartItem==null) {
            return R.error("添加失败");
        }

        return R.ok("添加成功");
    }

    /**
     * 改变选中状态
     * @param cartList
     * @return
     */
    @PostMapping("/itemCheck")
    public R itemCheck(@RequestBody List<CartChangeVo> cartList){
        System.out.println(cartList);
        if (cartList!=null && cartList.size()>0){
            shopCartService.itemCheck(cartList);
            return R.ok();
        }else{
            return R.error("数据出错");
        }
    }

    /**
     * 删除购物车购物项
     * @param goodsIds
     * @return
     */
    @PostMapping("/delForCart")
    public R delForCart(@RequestBody Long[] goodsIds){
        if(goodsIds!=null && goodsIds.length>0){
            shopCartService.delForCart(goodsIds);
            return R.ok();
        }else{
            return R.error("数据出错");
        }
    }
}
